<?php

require_once dirname(__FILE__) . '/Dice/SixSided.php';

class Dice
{
    private $_dice = array();

    public function __construct($type, $quantity)
    {
        for ($i = 0; $i < $quantity; $i++) {
            try {
                $dice_name = 'Dice_' . $type;
                $die = new $dice_name();
            } catch(ErrorException $e) {
                throw new Exception("'$type' is an invalid die type");
            }

            $this->addDie($die);
        }
    }

    public function addDie($die)
    {
        $this->_dice[] = $die;
    }

    public function getDice()
    {
        return $this->_dice;
    }

    public function roll()
    {
        $result = 0;

        foreach($this->getDice() as $die) {
            $result += $die->roll();
        }

        return $result;
    }
}
